package com.escom.service;

import android.util.Log;

import com.androidplot.xy.XYSeries;
import com.escom.utils.Constantes;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

/**
 * Created by Carlos on 21/04/2014.
 */
public class PlotAirflowDynamic implements Runnable, XYSeries {

    private PlotObservable plotObservable;
    private List<Double> valueAirflow;
    private int sizeEje;
    private static boolean run = true;

    public PlotAirflowDynamic() {
        plotObservable = new PlotObservable();
        sizeEje = 30;
        initArray();
    }

    class PlotObservable extends Observable {

        @Override
        public void notifyObservers() {
            setChanged();
            super.notifyObservers();
        }
    }

    public void stopThread() {
        run = false;
    }

    @Override
    public void run() {
        try {
            //while (run) {
            int i = 0;
            byte[] buffer = new byte[32];
            MulticastSocket s = new MulticastSocket(Constantes.PORT_AIRFLOW);
            s.joinGroup(InetAddress.getByName(Constantes.GROUP));
            DatagramPacket pack = new DatagramPacket(buffer, buffer.length);
            while (true) {
                s.receive(pack);
                byte[] data = pack.getData();
                String valueByte = new String(data);
                StringTokenizer stringTokenizer = new StringTokenizer(valueByte, ",");
                String valueString = stringTokenizer.nextToken();
                double value = Double.valueOf(valueString);
                if (value < 150) {
                    Log.d(Constantes.LOG_ID, this.getClass().getName() + " Valor: " + valueString);
                    valueAirflow.add(value);
                    valueAirflow.remove(0);
                    plotObservable.notifyObservers();
                }
                //Thread.sleep(100); // decrease or remove to speed up the refresh rate
            }
            //}
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return sizeEje;
    }

    @Override
    public Number getX(int i) {
        return i;
    }

    @Override
    public Number getY(int i) {
        return valueAirflow.get(i);
    }

    @Override
    public String getTitle() {
        return "Airflow";
    }

    public void initArray() {
        valueAirflow = new ArrayList<Double>();
        for (int i = 0; i < sizeEje; i++) {
            valueAirflow.add(0.0);
        }
    }

    public void addObserver(Observer observer) {
        plotObservable.addObserver(observer);
    }

}
